package levelrewind.android.app.adapter;

import java.util.ArrayList;

import com.greystripe.sdk.GSAd;
import com.greystripe.sdk.GSAdErrorCode;
import com.greystripe.sdk.GSAdListener;
import com.greystripe.sdk.GSFullscreenAd;

import levelrewind.android.app.DisplayBattleActivity;
import levelrewind.android.app.MainActivity;
import levelrewind.android.app.R;
import levelrewind.android.app.model.PictureHandler;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.vo.Game;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class GameListAdapter extends BaseAdapter {

	private ArrayList<Game>  games = new ArrayList<Game>();
    private LayoutInflater mInflater;
    private Context context;
	private GSFullscreenAd myFullscreenAd;
	private Game curGame;
	
	
    public GameListAdapter(Context context) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }
    
    public void addItem(final Game item) {
    	games.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return games.size();
    }

    @Override
    public Game getItem(int position) {
        return games.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.game_list, null);
            holder = new ViewHolder();
            holder.gameIcon = (ImageView)convertView.findViewById(R.id.game_icon);
            holder.txtName = (TextView)convertView.findViewById(R.id.game_name);
            holder.txtDesc = (TextView)convertView.findViewById(R.id.game_desc);
            holder.playBtn = (Button)convertView.findViewById(R.id.play_button);
            
            PictureHandler ph = new PictureHandler(context);
            holder.gameIcon.setImageDrawable(ph.getPictureFromDevice(games.get(position).getName()));
            holder.txtName.setTypeface(Util.customFont(context));
            holder.txtDesc.setTypeface(Util.customFont(context));
            holder.playBtn.setTypeface(Util.customFont(context));
            
            // Set text color for difference activities
            if ( context instanceof MainActivity) {
            	holder.txtName.setTextColor(context.getResources().getColor(R.color.white));
            	holder.txtDesc.setTextColor(context.getResources().getColor(R.color.white));
    		}
    		else if ( context instanceof DisplayBattleActivity) {
    			holder.txtName.setTextColor(context.getResources().getColor(R.color.blackBackground));
    			holder.txtDesc.setTextColor(context.getResources().getColor(R.color.blackBackground));
    		}
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }        
        
        holder.txtName.setText(games.get(position).getName());
        holder.txtDesc.setText(games.get(position).getDescription());
        
        //holder.playBtn.setTag(games.get(position));
        holder.playBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				curGame = games.get(position);
		    	Log.i(this.getClass().getSimpleName(),"Game Clicked: "+ curGame.getId()+"");
		    	//Add banner before game start
		    	addFullScreenAdBanner(context);
			}
		});
        
        return convertView;
    }
    
    private void addFullScreenAdBanner(Context c) {
		String gs_guid = "";
		try {
			ApplicationInfo ai = c.getPackageManager().getApplicationInfo(c.getPackageName(), PackageManager.GET_META_DATA);
			Bundle bundle = ai.metaData; 
			gs_guid = (String)bundle.get("gs_guid"); 
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        
		//Add full screen banner before the game start
        myFullscreenAd = new GSFullscreenAd(c, gs_guid);
        MyFullscreenListener myFullscreenAdListener = new MyFullscreenListener();
        myFullscreenAd.addListener(myFullscreenAdListener);
        myFullscreenAd.fetch();
	}
	 
	 public class MyFullscreenListener implements GSAdListener{

			@Override
			public void onAdClickthrough(GSAd arg0) {
				Log.i("GSAdListener", "onAdClickthrough");
				startGame();
				
			}

			@Override
			public void onAdDismissal(GSAd arg0) {
				//when you clicked on 'skip'
				Log.i("GSAdListener", "onAdDismissal");
				startGame();
				
			}

			@Override
			public void onFailedToFetchAd(GSAd arg0, GSAdErrorCode arg1) {
				Log.i("GSAdListener", "onFailedToFetchAd");
				startGame();
				
			}

			@Override
			public void onFetchedAd(GSAd arg0) {
				if (myFullscreenAd.isAdReady()) {
					myFullscreenAd.display();
				}
				
			}

		}
	
	private void startGame() {
		if ( context instanceof MainActivity) {
			MainActivity act = (MainActivity)context;
			act.startGame(curGame.getId());
		}
		else if ( context instanceof DisplayBattleActivity) {
			DisplayBattleActivity act = (DisplayBattleActivity)context;
			act.startGame(curGame.getId());
		}
	}

    public static class ViewHolder {
    	public ImageView gameIcon;
        public TextView txtName;
        public TextView txtDesc;
        public Button playBtn;
    }
    
    

}
